import { isArray } from 'lodash';
import { toJS } from 'mobx';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { searchPngImg, requiredPngImg } from '.';
import { Browser, ListPage, BrowserHrm, BrowserCity,RouteLayout } from 'weaver-mobile-page';
import { Tools,Button } from 'weaver-mobile';
const { getLabel} = Tools;

export default class WeaBrowserCellType extends window.GC.Spread.Sheets.CellTypes.Base {
    typeName = 'WeaBrowserCellType';
    fieldType;
    browserType;
    browserTitile;
    dataParams = {};
    tabs = [];
    viewAttr = 2;
    label;
    expandType;
    combineSameCell;
    clickwidth;
    paint(ctx, value, x, y, w, h, style, context) {
        if (!ctx) {
            return;
        }
        if (this.viewAttr != 1) {
            super.paint(ctx, value, x, y, w - 20, h, style, context);
            super.paint(ctx, '', x + w - 20, y, 20, h, style, context);
            const width = Math.min(16, h);
            if (this.viewAttr == 2) {
                this.clickwidth = width;
                ctx.drawImage(searchPngImg, x + w - width, y + (h - width) / 2, width, width);
            } else if (this.viewAttr == 3) {
                if (value && value.length > 0) {
                    this.clickwidth = width;
                    ctx.drawImage(searchPngImg, x + w - width, y + (h - width) / 2, width, width);
                } else {
                    this.clickwidth = width + 9;
                    ctx.drawImage(searchPngImg, x + w - this.clickwidth, y + (h - width) / 2, width, width);
                    ctx.drawImage(requiredPngImg, x + w - 8, y + (h - 7) / 2, 7, 7);
                }

            }

        } else {
            super.paint(ctx, value, x, y, w, h, style, context);
        }

    }
    format(value, format, formattedData, context) {
        if (isArray(value)) {
            return value.filter(d => typeof d !== 'function').map(d => this.delHtmlTag(d.names || d.name)).join(',');
        } else {
            return '';
        }
    }
    fromJSON(setting) {
        this.fieldType = toJS(setting.fieldType);
        this.viewAttr = setting.viewAttr;
        this.label = setting.label;
        this.browserType = this.fieldType[1].value;
        this.expandType = setting.expandType;
        this.combineSameCell = setting.combineSameCell;
        if (this.fieldType.length == 2) {

            this.browserTitile = this.fieldType[1].valueSpan;
        } else {
            if (this.fieldType[1]["valueObj"]) {
                this.browserTitile = this.fieldType[1]["valueObj"][0].name;
            } else {
                this.browserTitile = this.fieldType[1].valueSpan;
            }

        }

        if (this.browserType === '161' || this.browserType === '162'){
            let btype = (this.fieldType[2] || {}).value;
            if(btype && !btype.startsWith("browser.")) {
                btype = "browser."+ btype;
            }
            this.dataParams = { ...this.dataParams, type: btype}
        } else if( this.browserType === '256' || this.browserType === '257') {
            this.dataParams = { ...this.dataParams, cube_treeid: (this.fieldType[2] || {}).value }
        } else if(this.browserType == '171'){
            this.dataParams = {...this.dataParams, status:2};
        }
        if (this.browserType === '4' || this.browserType === '57' || this.browserType === '167' || this.browserType === '168') {
            this.tabs = {
                tabs: [{
                    "key": "2",
                    "name": getLabel('505968', '组织结构'),
                    "dataURL": null,
                    "dataParams": null,
                    "isSearch": false,
                    "showOrder": 0,
                    "selected": false
                }, {
                    "key": "1",
                    "name": getLabel('505969', '按列表'),
                    "dataURL": null,
                    "dataParams": {
                        "list": "1"
                    },
                    "isSearch": true,
                    "showOrder": 0,
                    "selected": false
                }]
            };
        } else if (this.browserType === '164' || this.browserType === '169' || this.browserType === '170' || this.browserType === '194') {
            this.tabs = {
                tabs: [{
                    "key": "2",
                    "name": getLabel('505968', '组织结构'),
                    "dataURL": null,
                    "dataParams": null,
                    "isSearch": false,
                    "showOrder": 0,
                    "selected": false
                }, {
                    "key": "1",
                    "name": getLabel('505969', '按列表'),
                    "dataURL": null,
                    "dataParams": {
                        "list": "1"
                    },
                    "isSearch": true,
                    "showOrder": 0,
                    "selected": false
                }]
            };
        } else {
            this.tabs = {};
        }
        this.isSingle = setting.isSingle;
        this.isMultCheckbox = setting.isMultCheckbox;
    }
    getHitInfo(x, y, cellStyle, cellRect, context) {
        const { row, col, sheetArea, sheet } = context;
        return {
            x,
            y,
            row,
            col,
            cellRect,
            sheetArea,
            sheet,
        };
    }
    processMouseUp(hitInfo) {
        // 只读 不展示
        if (this.viewAttr && this.viewAttr == 1) {
            return;
        }
        const { x, y, cellRect, sheet, row, col, sheetArea, reservedLocation, isReservedLocation } = hitInfo;
        if (x && cellRect) {
            if (x >= cellRect.x + cellRect.width - this.clickwidth) {
                this.showBrowserModal(sheet.getCell(row, col, sheetArea), cellRect);
            }
            return true;
        }
        return super.processMouseUp(hitInfo);
    }

    delHtmlTag(str) {
        if (str) {
            //去掉所有的html标记
            return str.replace(/<[^>]+>/g, "");
        }
    }
    
    showBrowserModal(context, cellRect) {
        // 只读 不展示
        if (this.viewAttr && this.viewAttr == 1) {
            return;
        }
        const { sheet, row, col } = context;

        
        this.switchValue(sheet.getCell(row, col).value());

        window.browserAllBrowserStore.currentCell = sheet.getCell(row, col);
        window.browserAllBrowserStore.currentValue = sheet.getCell(row, col).value();
        window.browserAllBrowserStore.type = this.browserType;
        window.browserAllBrowserStore.isSingle = this.isSingle;
        
        window.browserAllBrowserStore.dataParams = this.dataParams;
        window.browserAllBrowserStore.dataParamsObj = this.dataParams;
        
        this.showBytype();
        
    }

    showBytype() {
        this.browserType;

        switch (this.browserType) {

            case '1':
            case '17':
            window.excelHistory.push(window.browserAllBrowserStore.browserHrm.path);
            break;
            case '258':
            window.excelHistory.push(window.browserAllBrowserStore.browserCitygj.path);
            break;
            case '2222':
            window.excelHistory.push(window.browserAllBrowserStore.browserCitysf.path);
            break;
            case '58':
            window.excelHistory.push(window.browserAllBrowserStore.browserCitycs.path);
            break;
            case '263':
            window.excelHistory.push(window.browserAllBrowserStore.browserCityqx.path);
            break;
            default:
            window.excelHistory.push(window.browserAllBrowserStore.browser.path);
            break;

        }

        
    }

    switchValue(value) {
        switch (this.browserType) {

            case '178':
                window.browserAllBrowserStore.setDafalutValue((value || []).map(d => ({ randomFieldId: d.id, yearName: this.delHtmlTag(d.name) })));
            default:
                window.browserAllBrowserStore.setDafalutValue((value || []));
        }
    }
}